package com.example.superoa.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.example.superoa.model.pojos.Rapply;
import com.example.superoa.model.service.MenuService;
import com.example.superoa.model.service.RcountService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
 
@Controller
public class RcountController {
		@Autowired
		private RcountService countservice; 
		
		@Autowired
		private MenuService m_ser;
		
		//查询所有	
		@PostMapping("select-apply")
		@ResponseBody
		public Map<String,Object> showcount(String Staff_name,int page,int rows) {
			Map<String,Object> map = new HashMap<>();
			//先分页
			Page<Object> pg = PageHelper.startPage(page, rows);
			JSONArray j = countservice.findByNamecount(Staff_name);
			int i=1;
			for (Object object : j) {
				JSONObject obj=(JSONObject)object;
				obj.put("apply", i);
				i++;
			}
			//总记录
			map.put("total", pg.getTotal());
			//页内容
			map.put("rows", j);
			return map;
		}	
		/**新增
		 * @return
		 */
		@RequestMapping("recruitcounts")
		@ResponseBody
		public String applyinsert(Rapply apply,Integer id,Integer rplanid,HttpSession session) {
			Integer ids=Integer.parseInt(session.getAttribute("sid").toString());
			JSONObject json=m_ser.selectStaff(ids);
			countservice.insertrapply(apply,json.getInteger("staff_id"),ids,rplanid);
			return "redirect:main-administrator:recruitcount";				
		}
		//根据id查看
		@RequestMapping("rapply-look")
		@ResponseBody
		public JSONArray applyxiangq(int rapplyid) {
			return countservice.findByIdapply(rapplyid);
		}
		//初始化
		@RequestMapping("apply-allapply")
		@ResponseBody
		public JSONArray selectallapply(int rapplyid) {	
			return countservice.findByIdapply(rapplyid);
		}
		//编辑
		@RequestMapping("apply-xg")
		public void bianjisapply(int rapplyid,int rapplynums,Date rapplystart,Date rapplyend) {
			System.err.println("接收到"+rapplyid+".."+rapplynums+".."+rapplystart+".."+rapplyend);
			countservice.xiugaiapply(rapplyid, rapplynums, rapplystart, rapplyend);
		}
}